---@class InvasionTeamItem : UIComponentBase
local InvasionTeamItem = Class("InvasionTeamItem",UIComponentBase)
local t = InvasionTeamItem

function t:OnCreate()
    --self.playerIcon = self:GetChild("player/head/HeadIcon")
    --self.playerFrame = self:GetChild("player/frame")
    self.playerName = self:GetChild("playerName")
    self.ctrlLeader = self:GetController("leader")
    self.ctrlState = self:GetController("state")
    self.ctrlType = self:GetController("type")
    self.leaveCtrl=self:GetController("ifShowLeave")
    self.meCtrl=self:GetController("ifMe")
    self.btnLeave=self:GetChild("btnLeave")
    self.teamList={}
    for i=1,6 do
        local head=self:GetChild("Head"..i)
        table.insert(self.teamList,head)
    end
end

function t:OnEnable()

end
function t:OnDisable()
end
function t:OnDestroy()
end
function t:Empty()
    self:RefreshHeads(nil)
    self:SetTeamState(1)
    self.leaveCtrl.selectedIndex=0
end
function t:GetListPosInRoot()
    --return self.fguiObj.x + self.teamList.x,self.fguiObj.y + self.teamList.y
    return self.fguiObj.x,self.fguiObj.y
end
function t:SetCtrlType(type)
    self.ctrlType.selectedIndex = type
end
---@param player TBS.Player
function t:SetPlayer(player)
    if table.isNullOrEmpty(player)then
        self:Empty()
        return
    end
    --self:SetTeamState(0)
    self.groupId = player:GetGroupId()
    self.playerName.text = player.name
    self.heroList = TBS.ActorMgr.GetGroupActors(self.groupId)--player.heroList
    local playerId=tostring(player:GetPlayerId())
    local isMe=logicMgr.HeroManager.CompareMineId(playerId)
    if isMe then
        self.meCtrl.selectedIndex=1
    else
        self.meCtrl.selectedIndex=0
    end
    self.btnLeave.onClick:Clear()
    if InvasionHandler.IsMyRoom() and not isMe then
        self.leaveCtrl.selectedIndex=1
        self.btnLeave.onClick:Add(function()
            showUI(PanelNames.UICommonPop,"68",function(flag)
                if flag then
                    InvasionHandler.ReqKickOutTeamBoss(tostring(playerId) )
                end
            end,player.name)
        end)
    else
        self.leaveCtrl.selectedIndex=0
    end
    self:RefreshHeads(self.heroList,isMe)
end
function t:SetTeamState(state,player)
    self.ctrlState.selectedIndex = state
    if state==0 then
        state=1
    elseif state==1 then
        state=0
    end
    for i=1,6 do
        local go=self.teamList[i]
        local ctrlState = go:GetController("state")
        if table.isNullOrEmpty(player)then
            ctrlState.selectedIndex=0
        else
            local heroList = TBS.ActorMgr.GetGroupActors( player:GetGroupId())or{}
            if table.isNullOrEmpty(heroList[i])then
                ctrlState.selectedIndex=0
            else
                ctrlState.selectedIndex=state
            end
        end
    end
end
function t:SetTeamDead(groupIndex)
    if groupIndex > #self.teamList then
        logError("下标越界 "..groupIndex.." > "..#self.teamList)
        return
    end
    local child = self.teamList[groupIndex]
    local liveCtrl = child:GetController("live")
    liveCtrl.selectedIndex = 1
    self:SetTeamHp(groupIndex,0)
end
function t:AllAttack()
    local actors = TBS.ActorMgr.GetGroupActors(self.groupId)
    for k,v in pairs(actors) do
        if not v:IsDead() then
            local go = self:GetTeamItem(v:GetGroupIndex())
            local transition = go:GetTransition("attack")
            transition:Play()
        end
    end
end
---@return GComponent
function t:GetTeamItem(groupIndex)
    local child = self.teamList[groupIndex]
    return child
end
function t:SetTeamHp(groupIndex,percent)
    local child = self.teamList[groupIndex]
    local bar = child:GetChild("bar")
    bar.value = percent
end
function t:GetTeamItemPos(groupIndex)
    local child = self.teamList[groupIndex]
    local x = child.x
    local y = child.y
    return x,y
end
---@param go GComponent
function t:RefreshHeads(heroList,isMe)
    for i=1,6 do
        local go=self.teamList[i]
        local ctrlState = go:GetController("state")
        local icon = go:GetChild("icon"):GetChild("icon")
        local bar = go:GetChild("bar")
        local barCtrl=bar:GetController("type")
        local liveCtrl = go:GetController("live")
        liveCtrl.selectedIndex = 0
        if table.isNullOrEmpty(heroList) then
            ctrlState.selectedIndex = 0
            barCtrl.selectedIndex=1
        else
            if isMe then
                barCtrl.selectedIndex=0
            else
                barCtrl.selectedIndex=1
            end
            local data = heroList[i]
            if data == nil then
                ctrlState.selectedIndex = 0
            else
                bar.value = 100
                ctrlState.selectedIndex = 1
                local path = dataTable.getDataTableByStringId("t_hero",data.modelId,"f_HeroIcon")
                self:SetIcon(icon,path,EnumConst.ItemShowType.ICON)
            end
        end
    end
end
return t